﻿using Neo4jClient;
using System;

namespace Neo4j.DataProvider
{
    /// <summary>
    /// Neo4j graf client
    /// </summary>
    public static class Neo4jGrafClient
    {
        private static GraphClient _client;

        /// <summary>
        /// Graf client
        /// </summary>
        /// <param name="connectionString">Neo4j base connection string</param>
        /// <remarks>
        /// You should only have one instance of it for each database that you want to talk to (typically, one). This avoid
        /// excess calls to the Connect() method which requires a roundtrip to the neo4j server
        /// </remarks>
        public static GraphClient Instance(string connectionString)
        {
            if (_client == null)
            {
                _client = new GraphClient(new Uri(connectionString));
                try
                {
                    _client.Connect();
                }
                catch (AggregateException ex)
                {
                    _client = null;
                }
                return _client;
            }
            else
                return _client;
        }
    }
}
